/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication13;

import java.io.File;
import java.util.regex.*;


/**
 *
 * @author Irina
 */
public class Find implements ICommandExecutor  
{
    private static ICommandExecutor instance;
    
    @Override
    public void execute(String str)
    {
        String[] strOut=str.split(" ");
        if (strOut.length<2)
        { 
            System.out.println("Не были введены адрес и/или маска для поиска. Повторите попытку.");
        }
        else
        {
            String path=strOut[1];
            String mask=strOut[2];
           
            File fl=new File(path);
            if (!fl.exists()) 
            {
               System.out.println("Путь для поиска указан не верно");
            }
            else 
            {
                if(mask.contains("*")){mask.replace("*",".*"); FindWithRegex(fl,mask);}
                else{FindOnMask(fl,mask);} 
                System.out.println("Поиск завершен!");
                System.out.println();
             } 
        }       
    }
    
    private void FindOnMask(File fl,String mask)
    {
        File [] files=fl.listFiles();
        for(File elem:files)
        {
            if (elem.isFile())
            {
                String fileName = elem.getName();
                int i = fileName.lastIndexOf('.');
                if (i != -1) {fileName = fileName.substring(0, i);}
                if (mask.equals(fileName)){System.out.println("<file> "+elem.getName());}
            }
            if(elem.isDirectory())
            {
                if(mask.equals(elem.getName()))
                {
                    System.out.println("<dir> "+elem.getName());
                }
                FindOnMask(elem,mask);
            }
        }
    }
    
    private void FindWithRegex(File fl, String mask)
    {
        File [] files=fl.listFiles();
        Pattern p=Pattern.compile(mask);
        Matcher m;
        
        for(File elem:files)
        {
            if (elem.isFile())
            {
                String fileName = elem.getName();
                int i = fileName.lastIndexOf('.');
                if (i != -1) {fileName = fileName.substring(0, i);}
                m=p.matcher(fileName); 
                boolean searchResult=m.matches();
                if (searchResult)
                {
                    System.out.println("<file> "+elem.getName());
                }
            }
            if(elem.isDirectory())
            {
                m=p.matcher(elem.getName()); 
                boolean searchResult=m.matches();
                if (searchResult)
                {
                    System.out.println("<dir> "+elem.getName());
                }
                FindWithRegex(elem,mask);
            }
        }
    }
 
    public static ICommandExecutor getInstance()
    { 
         if(instance==null)
        {
            instance=new Find();
        }
        return instance;
        
    }       
}
    

